package com.hzz.service.river;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzz.api.river.IRiverInfoService;
import com.hzz.dao.river.IRiverInfoDao;
import com.hzz.dto.analysis.RiverInfoStatDto;
import com.hzz.dto.river.RiverInfoDto;
import com.hzz.entity.river.RiverInfo;
import com.hzz.framework.Response;
import com.hzz.framework.SysErrorCode;
import com.hzz.framework.annotation.FeignService;
import com.hzz.framework.exception.ServiceException;
import com.hzz.framework.service.BaseService;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.IdUtil;

import lombok.extern.slf4j.Slf4j;

@FeignService
@Slf4j
public class RiverInfoService extends BaseService implements IRiverInfoService {
	@Autowired
	IRiverInfoDao riverInfoDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {
			Exception.class, RuntimeException.class })
	public Response saveOrUpdateData(RiverInfoDto dto) throws Exception {
		Response result = new Response(0, "success");
		try {
			// TODO if (dto == null)throw new RuntimeException("参数异常!");
			RiverInfo entity = copyTo(dto, RiverInfo.class);
			// 判断数据是否存在
			if (riverInfoDao.isDataExist(entity) != 0) {
				// 数据存在
				riverInfoDao.update(entity);
			} else {
				// 新增
				if (entity.getId() == null) {
					entity.setId(IdUtil.nextId());
				}
				riverInfoDao.insert(entity);
				result.data = entity.getId();
			}
		} catch (Exception e) {
			log.error("信息保存异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	public String deleteData(RiverInfoDto dto) throws Exception {
		String result = "success";
		try {
			// TODO if (dto == null)throw new RuntimeException("参数异常!");
			RiverInfo entity = copyTo(dto, RiverInfo.class);
			if (riverInfoDao.deleteByPrimaryKey(entity) == 0) {
				throw new RuntimeException("数据不存在!");
			}
		} catch (Exception e) {
			log.error("物理删除异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {
			Exception.class, RuntimeException.class })
	public String deleteDataById(RiverInfoDto dto) throws Exception {
		String result = "success";
		try {
			// TODO if (dto == null)throw new RuntimeException("参数异常!");
			RiverInfo entity = copyTo(dto, RiverInfo.class);
			if (riverInfoDao.deleteById(entity) == 0) {
				throw new RuntimeException("数据不存在!");
			}
		} catch (Exception e) {
			log.error("逻辑删除异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	public PageInfo findDataIsPage(RiverInfoDto dto) throws Exception {
		PageInfo pageInfo = null;
		try {
			// TODO if (dto == null)throw new RuntimeException("参数异常!");
			RiverInfo entity = copyTo(dto, RiverInfo.class);
			PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
			List list = riverInfoDao.findDataIsPage(entity);
			pageInfo = new PageInfo(list);
			pageInfo.setList(copyTo(pageInfo.getList(), RiverInfoDto.class));
		} catch (Exception e) {
			log.error("信息[分页]查询异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return pageInfo;
	}

	@Override
	public List<RiverInfoDto> findDataIsList(RiverInfoDto dto) throws Exception {
		List<RiverInfoDto> results = null;
		try {
			RiverInfo entity = copyTo(dto, RiverInfo.class);
			results = copyTo(riverInfoDao.findDataIsList(entity), RiverInfoDto.class);
		} catch (Exception e) {
			log.error("信息[列表]查询异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return results;
	}

	@Override
	public RiverInfoDto findDataById(RiverInfoDto dto) throws Exception {
		RiverInfoDto result = null;
		try {
			RiverInfo entity = copyTo(dto, RiverInfo.class);
			result = copyTo(riverInfoDao.selectByPrimaryKey(entity), RiverInfoDto.class);
		} catch (Exception e) {
			log.error("信息[详情]查询异常!", e);
			throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
		}
		return result;
	}

	@Override
	public List<RiverInfoStatDto> statLevelNumByParentAreaId(Long currAreaId) {
		return riverInfoDao.statLevelNumByParentAreaId(currAreaId);
	}

	@Override
	public PageInfo<RiverInfoStatDto> statNameNumByParentAreaId(RiverInfoDto dto) throws Exception {
		RiverInfo entity = copyTo(dto, RiverInfo.class);
		PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
		List<RiverInfoStatDto> list = riverInfoDao.statNameNumByParentAreaId(entity);
		PageInfo<RiverInfoStatDto> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public int getTotalNum(Integer type) {
		return riverInfoDao.getTotalNum(type);
	}

}
